package zz.note.satoken.utils;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

public class ResultModel extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    public ResultModel() {
        put("code", "00000");
        put("msg", "success");
    }

    /**
     * 错误码列表
     */
    @Getter
    @AllArgsConstructor
    public enum ErrorCode{
        A0001("A0001","用户端错误"),
        A0100("A0100","用户注册错误"),
        A0200("A0200","用户登陆异常"),  //二级宏观错误
        A0201("A0201","用户账户不存在"),
        A0210("A0210","用户密码错误"),
        A0300("A0300","访问权限异常"),
        A0301("A0301","访问未授权"),
        A0400("A0400","用户请求参数错误"),
        C0001("C0001","调用第三方服务出错");  //一级宏观错误

        private String code;
        private String msg;
    }

    public static ResultModel error() {
        return error("500", "未知异常，请联系管理员");
    }

    public static ResultModel error(String msg) {
        return error("500", msg);
    }


    public static ResultModel error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

    public static ResultModel error(String code, String msg) {
        ResultModel r = new ResultModel();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static ResultModel ok(String msg) {
        ResultModel r = new ResultModel();
        r.put("msg", msg);
        return r;
    }

    public static ResultModel ok(Map<String, Object> map) {
        ResultModel r = new ResultModel();
        r.putAll(map);
        return r;
    }

    public static ResultModel ok() {
        return new ResultModel();
    }

    @Override
    public ResultModel put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
